package fi.foyt.cs.persistence.dao.user;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import fi.foyt.cs.persistence.dao.GenericDAO;
import fi.foyt.cs.persistence.domainmodel.user.UserFriend;

public class UserFriendDAO extends GenericDAO<UserFriend> {
  
  public UserFriend create(Long userId, Long friendId, Boolean confirmed) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      UserFriend userIdentifier = new UserFriend();
      userIdentifier.setFriendId(friendId);
      userIdentifier.setUserId(userId);
      userIdentifier.setConfirmed(confirmed);
      entityManager.persist(userIdentifier);
      entityManager.getTransaction().commit();
      return userIdentifier;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }
  
  public UserFriend findByUserIdAndFriendId(Long userId, Long friendId) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from UserFriend o where userId = :userId and friendId = :friendId");
    query.setParameter("userId", userId);
    query.setParameter("friendId", friendId);
    return getSingleResult(query);
  }
  
  @SuppressWarnings("unchecked")
  public List<UserFriend> listByUserId(Long userId) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from UserFriend o where userId = :userId");
    query.setParameter("userId", userId);
    return query.getResultList();
  }
  
  @SuppressWarnings("unchecked")
  public List<UserFriend> listByUserIdAndConfirmed(Long userId, Boolean confirmed) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from UserFriend o where userId = :userId and confirmed = :confirmed");
    query.setParameter("userId", userId);
    query.setParameter("confirmed", confirmed);
    return query.getResultList();
  }
  
  @SuppressWarnings("unchecked")
  public List<UserFriend> listByFriendId(Long friendId) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from UserFriend o where friendId = :friendId");
    query.setParameter("friendId", friendId);
    return query.getResultList();
  }
  
  @SuppressWarnings("unchecked")
  public List<UserFriend> listByFriendIdAndConfirmed(Long friendId, Boolean confirmed) {
    EntityManager entityManager = getEntityManager();
    Query query = entityManager.createQuery("select o from UserFriend o where friendId = :friendId and confirmed = :confirmed");
    query.setParameter("friendId", friendId);
    query.setParameter("confirmed", confirmed);
    return query.getResultList();
  }

  public UserFriend updateConfirmed(UserFriend userFriend, Boolean confirmed) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      userFriend.setConfirmed(confirmed);
      entityManager.persist(userFriend);
      entityManager.getTransaction().commit();
      return userFriend;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }
}
